package thread_pool.schedule;

import org.junit.Test;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/19 3:15 PM
 */
public class ScheduledThreadPoolExecutorTest {

    @Test
    public void test1() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10, r -> new Thread(r, "test1"));

        printTime();

        executor.execute(() -> {
            printTime();
        });

        printTime();


    }

    @Test
    public void test2() throws IOException {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10, r -> new Thread(r, "test2"));

        printTime();

        // 指定延迟后执行一次
        executor.schedule(() -> printTime(), 3, TimeUnit.SECONDS);

        printTime();

        System.in.read();

    }


    @Test
    public void test3() throws IOException {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10, r -> new Thread(r, "test3"));

        printTime();

        // 指定延迟后执行，然后每隔 n 循环执行
        // 每次执行的开始时刻相差 : period * TimeUnit
        executor.scheduleAtFixedRate(() -> printTime(), 5, 3, TimeUnit.SECONDS);

        printTime();

        System.in.read();

    }

    /**
     * main ---> 2019-04-19 15:48:07.907
     * main ---> 2019-04-19 15:48:08.420
     * test3 ---> 2019-04-19 15:48:13.426
     * test3 ---> 2019-04-19 15:48:16.418
     * test3 ---> 2019-04-19 15:48:19.423
     * test3 ---> 2019-04-19 15:48:22.426
     * test3 ---> 2019-04-19 15:48:25.424
     * test3 ---> 2019-04-19 15:48:28.422
     * test3 ---> 2019-04-19 15:48:31.419
     * test3 ---> 2019-04-19 15:48:34.421
     * test3 ---> 2019-04-19 15:48:37.423
     */


    @Test
    public void test4() throws IOException {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10, r -> new Thread(r, "test4"));

        printTime();

        // 指定延迟后执行，执行完成后每隔 n 循环执行
        // 每次执行的开始时刻相差 : period * TimeUnit + run()方法执行时间
        executor.scheduleWithFixedDelay(() -> printTime(), 5, 3, TimeUnit.SECONDS);

        printTime();

        System.in.read();

    }

    /**
     * main ---> 2019-04-19 15:49:14.265
     * main ---> 2019-04-19 15:49:14.782
     * test4 ---> 2019-04-19 15:49:19.786
     * test4 ---> 2019-04-19 15:49:23.293
     * test4 ---> 2019-04-19 15:49:26.798
     * test4 ---> 2019-04-19 15:49:30.303
     * test4 ---> 2019-04-19 15:49:33.812
     * test4 ---> 2019-04-19 15:49:37.322
     * test4 ---> 2019-04-19 15:49:40.828
     * test4 ---> 2019-04-19 15:49:44.333
     * test4 ---> 2019-04-19 15:49:47.841
     * test4 ---> 2019-04-19 15:49:51.347
     */


    private static void printTime() {
        try {
            Thread.sleep(2900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " ---> " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS ")));
    }
}
